package io.github.jyrmc.deploy.plugin

import io.github.jyrmc.deploy.common.TaskConstant
import io.github.jyrmc.deploy.extension.DeployJavaApplicationExtension
import io.github.jyrmc.deploy.extension.DeploymentExtension
import io.github.jyrmc.deploy.task.DeployJavaApplicationTask
import io.github.jyrmc.deploy.task.DeployToDockerTask
import io.github.jyrmc.deploy.task.DeployToKubernetesTask
import io.github.jyrmc.deploy.task.SearchPodTask
import org.gradle.api.Project

/**
 * plugin
 *
 * @author jy
 * @since 2023/5/16 11:23
 **/
class DeployPlugin : BasePlugin() {
    override fun apply(project: Project) {
        project.plugins.apply(DeployPlugin::class.java)
        val deploymentExtension = project.extensions.create<DeploymentExtension>("deploymentExtension")
        val javaApplicationExtension = project.extensions.create<DeployJavaApplicationExtension>("deployJavaApplicationExtension")

        project.plugins.withType(DeployPlugin::class.java).configureEach {
            registerDeployToKubernetesTask(project, deploymentExtension)
            registerGetPodStatusTask(project, deploymentExtension)
            registerDeployToDockerTask(project, deploymentExtension)
            registerDeployJavaApplicationTask(project, javaApplicationExtension)
        }
    }

    /**
     * 注册部署k8s task
     */
    private fun registerDeployToKubernetesTask(
        project: Project, extension: DeploymentExtension
    ): Any {
        return project.tasks.register(
            TaskConstant.DEPLOY_K8S_TASK_NAME, DeployToKubernetesTask::class.java
        ) {
            var dependsTaskName = extension.dependsTaskName.get()
            if (dependsTaskName.isBlank()) {
                dependsTaskName = "dockerPushImage"
            }
            dependsOn(dependsTaskName)
            this.group = TaskConstant.DEFAULT_TASK_GROUP
            this.description = "Deploy Current Application to Kubernetes Cluster"
            this.host = extension.host.get()
            this.port = extension.port.get()
            this.username = extension.username.get()
            this.password = extension.password.get()
            this.commandLine = extension.commandLine.get()
            this.imagePrefix = extension.imagePrefix.get()
            this.imageName = extension.imageName.get()
            this.imageVersion = extension.imageVersion.get()
            this.deploymentName = extension.deploymentName.get()
            this.namespace = extension.namespace.get()
        }
    }

    /**
     * 注册部署获取pod状态 task
     */
    private fun registerGetPodStatusTask(
        project: Project, extension: DeploymentExtension
    ): Any {
        return project.tasks.register(
            TaskConstant.POD_STATUS_TASK_NAME, SearchPodTask::class.java
        ) {
            this.group = TaskConstant.DEFAULT_TASK_GROUP
            this.description = "Get Current Application Pod Status On Kubernetes Cluster"
            this.host = extension.host.get()
            this.port = extension.port.get()
            this.username = extension.username.get()
            this.password = extension.password.get()
            this.imageName = extension.imageName.get()
            this.deploymentName = extension.deploymentName.get()
            this.namespace = extension.namespace.get()
        }
    }

    /**
     * 注册部署本地docker task
     */
    private fun registerDeployToDockerTask(
        project: Project, extension: DeploymentExtension
    ): Any {
        return project.tasks.register(
            TaskConstant.DEPLOY_DOCKER_TASK_NAME, DeployToDockerTask::class.java
        ) {
            var dependsTaskName = extension.dependsTaskName.get()
            if (dependsTaskName.isBlank()) {
                dependsTaskName = "dockerBuildImage"
            }
            dependsOn(dependsTaskName)
            this.group = TaskConstant.DEFAULT_TASK_GROUP
            this.description = "Deploy Current Application to Docker"
            this.host = extension.host.get()
            this.port = extension.port.get()
            this.username = extension.username.get()
            this.password = extension.password.get()
            this.imagePrefix = extension.imagePrefix.get()
            this.imageName = extension.imageName.get()
            this.imageVersion = extension.imageVersion.get()
        }
    }

    /**
     * register deploy java app task
     */
    private fun registerDeployJavaApplicationTask(
        project: Project, extension: DeployJavaApplicationExtension
    ): Any {
        return project.tasks.register(
            TaskConstant.DEPLOY_JAVA_APP_NAME, DeployJavaApplicationTask::class.java
        ) {
            var dependsTaskName = extension.dependsTaskName.get()
            if (dependsTaskName.isBlank()) {
                dependsTaskName = "bootJar"
            }
             dependsOn(dependsTaskName)
            this.group = TaskConstant.DEFAULT_TASK_GROUP
            this.description = "Deploy Current Java Application Server"
            this.host = extension.host.get()
            this.port = extension.port.get()
            this.username = extension.username.get()
            this.password = extension.password.get()
            this.localJarPath = extension.localJarPath.get()
            this.args = extension.args.get()
            this.deployPath = extension.deployPath.get()
            this.javaHome = extension.javaHome.get()
        }
    }
}



